package com.kingyea.app.market.appauthorization.controller;

import com.kingyea.app.market.Dictionary.Dictionary;
import com.kingyea.app.market.appauthorization.bean.AppAuthorization;
import com.kingyea.app.market.appauthorization.bean.AppAuthorizationPo;
import com.kingyea.app.market.appauthorization.bean.AppAuthorizationVo;
import com.kingyea.app.market.appauthorization.service.AppAuthorizationService;
import com.kingyea.app.market.client.DictionaryServiceServiceClient;
import com.kingyea.app.market.client.OrganizationServiceClient;
import com.kingyea.app.market.client.RoleServiceClient;
import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.dept.bean.Organization;
import com.kingyea.app.market.role.Role;
import com.kingyea.app.market.utils.ExceptionConditions;
import com.kingyea.app.market.utils.StateCode;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.system.aspect.NotResult;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hx
 * @version v1.0.0
 * @description AppAuthorization 控制层
 * @date Created in 2018-05-02 11:34:13
 */
@RequestMapping(value = "/appAuthorization")
@RestController
public class AppAuthorizationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppAuthorizationController.class);

    @Autowired
    private AppAuthorizationService appAuthorizationService;

    @Autowired
    private OrganizationServiceClient organizationServiceClient;

    @Autowired
    private RoleServiceClient roleServiceClient;

    @Autowired
    private UserServiceClient userServiceClient;

    @Autowired
    private DictionaryServiceServiceClient dictionaryServiceServiceClient;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return appAuthorizationService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public AppAuthorization detail(@PathVariable Integer id) throws Exception {
        return appAuthorizationService.getById(id);
    }

    @NotResult
    @PostMapping(value = "/search")
    public Object search(@RequestBody AppAuthorization appAuthorization) throws Exception {
        ExceptionConditions.checkNotNull(appAuthorization.getMarketCode(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkStringNotEmpty(appAuthorization.getAppCode(), StateCode.NO_ARGUMENT_ERROR);
//        ExceptionConditions.checkNotNull(appAuthorization.getType(), StateCode.NO_ARGUMENT_ERROR);
        PageInfo<AppAuthorization> appAuthorPage = appAuthorizationService.list(appAuthorization);
        if (appAuthorPage.getTotal() > 0) {
            List<AppAuthorizationVo> appVoList = appAuthorPage.getList().stream().map(bean -> {
                AppAuthorizationVo vo = new AppAuthorizationVo();
                BeanUtils.copyProperties(bean, vo);
                return vo;
            }).collect(Collectors.toList());
            appVoList.stream().forEach(vo -> {
                Result<User> userRut = userServiceClient.getByUserId(vo.getAuthorizationUserId());
                vo.setAuthName(userRut.getData() == null ? "" : userRut.getData().getName());
//                类型 1 部门，2 角色， 3 警种， 4 用户
                switch (vo.getType()) {
                    case 1:
                        Result<Organization> orgResult = organizationServiceClient.getBySerialNo(vo.getOrgSerialNo());
                        vo.setOrgName(orgResult.getData().getName());
                        vo.setTypeName("部门");
                        vo.setName(orgResult.getData().getName());
                        break;
                    case 2:
                        Result<Role> roleResult = roleServiceClient.findById(vo.getRoleId());
                        vo.setRoleName(roleResult.getData().getRoleName());
                        vo.setTypeName("角色");
                        vo.setName(roleResult.getData().getRoleName());
                        break;
                    case 3:
                        Result<Dictionary> policeResult = dictionaryServiceServiceClient.getById(vo.getPolice());
                        vo.setPoliceName(policeResult.getData().getName());
                        vo.setTypeName("警种");
                        vo.setName(policeResult.getData().getName());
                        break;
                    case 4:
                        Result<User> userResult = userServiceClient.getByUserId(vo.getUserId());
                        vo.setUserName(userResult.getData().getName());
                        vo.setTypeName("用户");
                        vo.setName(userResult.getData().getName());
                        break;
                }
            });
            appAuthorPage.setList(appVoList);
        }
        return appAuthorPage;
    }

    @NotResult
    @PostMapping(value = "/notPageSerach")
    public Object notPageSerach(@RequestBody AppAuthorization appAuthorization) throws Exception {
        return appAuthorizationService.list(appAuthorization);
    }


    @NotResult
    @PostMapping(value = "/createAll")
    public Object createAll(@RequestBody AppAuthorizationPo obj) throws Exception {
        return appAuthorizationService.createAll(obj);
    }

    /**
     * 创建
     *
     * @param appAuthorization
     * @return
     */
    @NotResult
    @RequestMapping(method = RequestMethod.POST)
    public Object create(@RequestBody AppAuthorization appAuthorization) throws Exception {
        return appAuthorizationService.save(appAuthorization);
    }

    /**
     * 更新
     *
     * @param appAuthorization
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@RequestBody AppAuthorization appAuthorization) throws Exception {
        return appAuthorizationService.update(appAuthorization);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public Object delete(@PathVariable Integer id) throws Exception {
        return appAuthorizationService.deleteById(id);
    }

    @RequestMapping(value = "/getbyUser", method = RequestMethod.POST)
    public Object getbyUser(@RequestBody AppAuthorizationPo appAuthorization) throws Exception {
        return appAuthorizationService.getbyUser(appAuthorization);
    }

}
